/**
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mycompany.project.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.storage.client.Storage;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.extjs.gxt.ui.client.widget.menu.MenuBar;
import com.extjs.gxt.ui.client.widget.menu.MenuBarItem;
import com.extjs.gxt.ui.client.widget.menu.Menu;
import com.extjs.gxt.ui.client.widget.menu.MenuItem;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.google.gwt.user.client.ui.DecoratedStackPanel;
import com.mycompany.project.client.chee.ConfigurationGUI;
import com.mycompany.project.client.chee.HabitatConfigurationGUI;
import com.mycompany.project.client.chee.LoggingModuleGUI;
import com.mycompany.project.client.chee.LoginScreen;
import com.mycompany.project.client.chee.MapGUI;
import com.mycompany.project.client.chee.Module;
import com.mycompany.project.client.chee.ModuleList;
import com.mycompany.project.client.duch.MiniRSGUI;
import com.mycompany.project.client.duch.RoverCalibrationGUI;
import com.mycompany.project.client.duch.Testing;
import com.mycompany.project.client.jack.Weather;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.event.MenuEvent;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 * @author Chee Lee, Nick Neville
 */
public class Main implements EntryPoint {

	/**
	 * Main Storage.
	 * @param private static Storage, local.
	 */
	private static Storage local = Storage.getLocalStorageIfSupported();

	/** LoginScreen: login. */
	private static LoginScreen login;

	/** LoggingModuleGUI: lgMD. */
	private static LoggingModuleGUI lgMD;

	/** AbsolutePanel: mainMCHS. */
	private AbsolutePanel mainMCHS = new AbsolutePanel();

	/** Menu: menu. */
	private Menu menu = new Menu();

	/** MenuBarItem: mnbrtmMchs. */
	private final MenuBarItem mnbrtmMchs = new MenuBarItem("MHCS", menu);

	/** HabitatConfigurationGUI: hCG. */
	private HabitatConfigurationGUI hCG = new HabitatConfigurationGUI();

	/** Weather. */
	private Weather weather = new Weather();
	/**
	 * minimum resource solution gui.
	 */
	private MiniRSGUI miniRSGUI = new MiniRSGUI();
	/**
	 * vertical panel for rovercalibrationgui.
	 */
	private static VerticalPanel rCGVPanel = new VerticalPanel();
	/**
	 * Switch button for switching between live map and configuration map.
	 */
	private Button switchBtn = new Button("Switch to Configuration Map");
	/**
	 * Vertical panel for the maps.
	 */
	private VerticalPanel mapVertical = new VerticalPanel();
	/**
	 * HTML label for the map.
	 */
	private HTML htmlmapArea = new HTML(
			"<center>Live Map Area (x , y)</center>",
			true);
	/**
	 * scroll panel for the live map.
	 */
	private ScrollPanel scrollPanel = new ScrollPanel();
	/**
	 * NASA/ESA feed.
	 */
	private Testing nasaFeed = new Testing();

	/**
	 * onModuleLoad() method.
	 */
	public final void onModuleLoad() {
		// initialize module list
		ModuleList.initMList();
		// initialize images
		Module.initImages();
		MenuItem mntmUser = new MenuItem("User");
		menu.add(mntmUser);
		Menu menu2 = new Menu();
		MenuItem mntmChangePicture = new MenuItem("Change picture");
		menu2.add(mntmChangePicture);
		MenuItem mntmChangePassword = new MenuItem("Change password");
		menu2.add(mntmChangePassword);
		MenuItem mntmChangeName = new MenuItem("Change name");
		menu2.add(mntmChangeName);
		mntmUser.setSubMenu(menu2);
		final RootPanel rootPanel = RootPanel.get();
		MenuItem mntmLogOut = new MenuItem("Logout");
		menu.add(mntmLogOut);

		// initialize login screen
		login = new LoginScreen(mainMCHS);

		// initialize map
		MapGUI.initMap();

		// --needs to be on when running actual program
		lgMD = new LoggingModuleGUI();

		// selectionListener of the Menu Item mntmLogOut
		mntmLogOut.addSelectionListener(
				new SelectionListener<MenuEvent>() {
			public void componentSelected(final MenuEvent ce) {
				Window.alert("Good bye!");
				rootPanel.remove(mainMCHS);
				rootPanel.add(login.getGUI());
			}
		});

		rootPanel.setSize("1041px", "641px");
		mainMCHS.setSize("1041px", "641px");

		MenuBar menuBar = new MenuBar();
		menuBar.add(mnbrtmMchs);
		mainMCHS.add(menuBar);
		menuBar.setSize("1064px", "20px");
		mainMCHS.setWidgetPosition(menuBar, 0, 0);


		ScrollPanel histogramScrollPanel
		= new ScrollPanel(lgMD.getHistogram());
		DecoratedStackPanel histoStack = new DecoratedStackPanel();
		histoStack.add(histogramScrollPanel);
		mainMCHS.add(histoStack, 707, 49);
		histogramScrollPanel.setSize("324px", "210px");
		histoStack.setSize("324px", "200px");
		HTML htmlhistogra
		= new HTML("<center><b>Histogram</b></center>", true);
		mainMCHS.add(htmlhistogra, 701, 27);
		htmlhistogra.setSize("330px", "16px");
		HTML htmlchat
		= new HTML("<center><b>Weather</b></center>", true);
		mainMCHS.add(htmlchat, 707, 291);
		htmlchat.setSize("324px", "16px");

		DecoratedTabPanel decoratedTabPanel = new DecoratedTabPanel();

		mainMCHS.add(decoratedTabPanel, 0, 26);
		decoratedTabPanel.setSize("675px", "569px");

		mapVertical.setVerticalAlign(VerticalAlignment.BOTTOM);
		mapVertical.setHorizontalAlign(HorizontalAlignment.CENTER);
		mapVertical.setSize("675px", "569px");
		mapVertical.add(htmlmapArea);


		scrollPanel.setSize("675px", "533px");

		scrollPanel.setWidget(MapGUI.getGrid());
		mapVertical.add(scrollPanel);
		mapVertical.add(switchBtn);
		initSwitchBtn();
		decoratedTabPanel.add(mapVertical, "Map", false);
		rCGVPanel.add(miniRSGUI.getGUI());
		rCGVPanel.setSize("675px", "569px");
		decoratedTabPanel.add(rCGVPanel,
				"<center>Min.Resource Solution</center>", true);

		VerticalPanel modulesVertical = new VerticalPanel();
		modulesVertical.setHorizontalAlign(HorizontalAlignment.CENTER);


		VerticalPanel grid1 = lgMD.getGUI();
		modulesVertical.add(grid1);
		grid1.setSize("633px", "262px");

		VerticalPanel habitatVPanel = new VerticalPanel();
		habitatVPanel.setSize("675px", "569px");
		habitatVPanel.add(hCG.getGUI());
		decoratedTabPanel.add(habitatVPanel,
				"<center>Habitat Configuration</center>", true);

		decoratedTabPanel.add(
				modulesVertical,
				"Logging Modules",
				false);

		modulesVertical.setSize("675px", "569px");

		decoratedTabPanel.selectTab(3);

		DecoratedStackPanel decoratedStackPanel
		= new DecoratedStackPanel();
		mainMCHS.add(decoratedStackPanel, 707, 313);
		decoratedStackPanel.setSize("334px", "324px");
		VerticalPanel weatherVpanel = new VerticalPanel();
		weatherVpanel.setSize("320px", "290px");
		weatherVpanel.add(weather.getPanel());
		weatherVpanel.setHorizontalAlign(HorizontalAlignment.CENTER);
		decoratedStackPanel.add(weatherVpanel,
				"<center></center>",
				true);

		HTML configurationLabel = lgMD.getConfigLabel();

		mainMCHS.add(configurationLabel, 707, 0);
		configurationLabel.setSize("324px", "20px");
		RoverCalibrationGUI.initRoverCalibration();
		decoratedTabPanel.add(nasaFeed.getHPanel(),
				"NASA/ESA Feed",
				false);
	}
	/**
	 * Initialized the switch button action listener.
	 */
	private void initSwitchBtn() {
		switchBtn.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(final ClickEvent event) {
				if(scrollPanel.getWidget() != MapGUI.getGrid()) {
					htmlmapArea.setHTML(
							"<center>Live Map Area"
							+ "(x , y)</center>");
					scrollPanel.
					remove(ConfigurationGUI.getConfigMap());
					scrollPanel.
					add(MapGUI.getGrid());
					switchBtn.
					setText("Switch to Configuration Map");
				} else {
					htmlmapArea.
					setHTML(
							"<center>Configuration Map Area (x , y)</center>");
					scrollPanel.remove(MapGUI.getGrid());
					scrollPanel.
					add(ConfigurationGUI.
							getConfigMap());
					switchBtn.setText("Switch to Live Map");
					ConfigurationGUI.showAllChanges();
				}
			}
		});
	}

	/**
	 * @return the LoggingModuleGUI.
	 */
	public static LoggingModuleGUI getLoggingModuleGUI() {
		return lgMD;
	}

	/**
	 * @return the login.
	 */
	public static LoginScreen getLogin() {
		return login;
	}

	/**
	 * @return the local.
	 */
	public static Storage getLocal() {
		return local;
	}

	/**
	 * @param loc
	 *            the local to set.
	 */
	public static void setLocal(final Storage loc) {
		Main.local = loc;
	}
}
